package com.example.mysql.controller;

import com.example.mysql.jpa.domain.Student;
import com.example.mysql.jpa.repository.StudentRepository;
import com.example.mysql.jpa.repository.TestRepository;
import com.example.mysql.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/student")
public class DruidController
{
    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private TestRepository testRepository;

    @RequestMapping("/get/{studentId}")
    public StudentVo getStudent(@PathVariable Long studentId)
    {
        if (studentId == null)
        {
            return new StudentVo();
        }

        testRepository.findAll(new Sort(Sort.Direction.ASC,"a"));
        Student student = studentRepository.findById(studentId.intValue()).orElse(null);
        if (student == null)
        {
            return new StudentVo();
        }

        StudentVo studentVo = new StudentVo();
        studentVo.setName(student.getName());
        studentVo.setId(student.getId().longValue());
        return studentVo;
    }
}
